import React from "react";
import _ from 'lodash';
import { Form, Select, Input, InputNumber, Button, Space, Cascader } from "antd";
import GoBack from "@/components/TheGoBack";
import { apiActivityCodeAdd } from '@/request/activityCode'
import "../../style/style.less";



class Add extends React.Component {
  state = {
    btnLoading: false,
    total : 1,
  };
  goback = () => {
    this.props.history.go(-1);
  };
  onSave = async (values) => {
    this.setState({ btnLoading: true })
    if(values.totalNum != null){
      this.setState({
        total : values.totalNum
      })
    }
    let params = {
      residueNum: values.residueNum,
      activityName: values.activityName
    }
    let code = ''
    let num = ''
    // console.log(params)
    for(let i = 0; i<this.state.total; i++){
      let res = await apiActivityCodeAdd({ ...params } )
      if(res.code == 0){
        code = res.code
        num = i+1
      }else{
        React.$message({
          type: 'info',
          content: res.codeMessage
        })
        code = 0
        num = i
        break;
      }
    }
    // let res = await apiActivityCodeAdd({ ...params } )
    if (code === 0) {
      React.$message({
        type: 'info',
        content: `成功生成${num}张二维码`
      })
      this.props.history.goBack()
    }
    this.setState({ btnLoading: false })
  }
  
  
  


  render() {
    const { btnLoading} = this.state;
    return (
      <div className="p-layout-box commodity-list-add">
        <GoBack history={this.props.history} title={"新增二维码"} />
        <div className="content">
          <div className="title">添加二维码</div>
          <div className="form-box">
            <p>如需一次生成多张二维码，请耐心等待，默认一次生成一张二维码</p>
            <Form ref={ref => this.formRef = ref} onFinish={this.onSave} >
              <Form.Item label="活动名称" name={'activityName'} rules={[{ required: true }]}>
                <Input placeholder="请输入活动名称" type='text' />
              </Form.Item>
              <Form.Item label="使用次数" name={'residueNum'} rules={[{ required: true }]}>
                <Input placeholder="请输入使用次数" type='number' />
              </Form.Item>
              <Form.Item label="生成数量" name={'totalNum'} rules={[{ required: false }]}>
                <Input placeholder="请输入生成数量" type='number' />
              </Form.Item>
              <Form.Item>
                <Button type="default" size="middle" onClick={this.goback}>
                  取消
                </Button>
                <Button
                  style={{ backgroundColor: "#F59A23", color: "#fff" }}
                  size="middle"
                  loading={btnLoading}
                  htmlType="submit"
                >
                  保存
                </Button>
              </Form.Item>
            </Form>
          </div>
        </div>
      </div>
    );
  }
}

export default Add;
